﻿using UnityEngine;
using System.Collections;
using System.IO;
namespace GameEditor
{
    public class MapEditorScene :BaseScene
    {
        GameObject SceneObject;
        public MapEditorPanel EditorPanel;
        MapEditorBlockView BlockViewUI;
        MapEditorMenuBar MenuBarUI;

        Hashtable EditorMapTable;
        public Hashtable MapTableIndex;
        public override void OnLoad(params object[] args)
        {
            //Init EditorPanel
            SceneObject = new GameObject("SceneObject");
            GameObject editorPanel = new GameObject("EditorPanel");
            editorPanel.transform.parent = SceneObject.transform;
            EditorPanel = editorPanel.AddComponent<MapEditorPanel>();
            
            MapTableIndex = (Hashtable)MapTableMgr.MapIndexTable[(int)args[0]];
            string mapFolder =  (string)MapTableIndex[":map-folder"];
            EditorMapTable = (Hashtable)SGLisp.Helper.EvalValueFromFile(GameEditor.MapFolder + mapFolder+"/map.clj", SGLisp.RT.MainContext);
            EditorPanel.OnLoad(EditorMapTable);
             
            //Init UI
            BlockViewUI = (MapEditorBlockView)GameEditor.UI.Show("MapEditorBlockView");
            MenuBarUI = (MapEditorMenuBar)GameEditor.UI.Show("MapEditorMenuBar");
            BlockViewUI.SelectIndexFn = EditorPanel.SelectBlockIndex;
            MenuBarUI.SaveItemEvent = SaveGameMap;
        }

        public void SaveGameMap()
        {
          EditorPanel.MapTable[":layers"] = EditorPanel.PanelLayersToMap();
          string mapStr =  SGLisp.Helper.DataToLispString(  EditorPanel.MapTable);
          string mapPath = GameEditor.MapFolder + (string)MapTableIndex[":map-path"];
          File.WriteAllText(mapPath,mapStr);
          Debug.Log("保存成功");
        }
    }
}